<?php
class Wechat{
	private $AppID="wx72e864184d9b223b";
	private $AppSecret="bdb217e74062ad4bf8d99f09bf9d2d49";
	private $AccessToken="";
	private $wxUserID="";
	private $dbaddress="localhost";
	private $dbuser="root";
	private $dbpsd="allenzhou103";
	private $dbname="web40";
	private $link;
	function __construct($wxUserID){
		$this->$wxUserID=$wxUserID;
		$this->getAccessToken();
		$this->connectDB();
	}
	public function getAccessToken(){
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->AppID."&secret=".$this->AppSecret;
		$result=$this->postJson($url,array());
		if($result->errcode==""){
			$this->AccessToken=$result->access_token;
		}else{
			$this->AccessToken=-1;
		}
		return $AccessToken;
	}
	public function connectDB(){
		$this->link = mysqli_connect($this->dbaddress, $this->dbuser, $this->dbpsd, $this->dbname);
	}
	public function checkBindStatus(){
		$result = mysqli_query($this->link,"SELECT COUNT(*) FROM userbind");

		$row = mysqli_fetch_array($result);

		return $row['COUNT(*)'];

	}
	public function bindUser($studentid,$password){
		$sql="insert into user (studentid,password) values('".$studentid."','".$password."')";
		mysqli_query($this->link,$sql);
		$sql="insert into userbind (wxuserid,studentid,bindtime)values('".$this->wxUserID."','".$studentid."','".time()."')";
		mysqli_query($this->link,$sql);
	}
	public function getGrade(){
		$url="http://192.168.0.101/web40/grade.php";
		$sql="select u.schoolid,u.password from user u inner join userbind b on u.wxuserid=b.wxuserid where u.wxuserid=".$this->wxUserID;
		$result=mysqli_query($this->link,$sql);
		$row = mysqli_fetch_array($result);
		$data=array(
			"username"=>$row[0],
			"password"=>$row[1],
		);
		$result=$this->postJson($url,$data);
		P($result);
	}
	/**
	 * 通过post请求数据
	 * @param string $url 要请求的链接
	 * @param array $data 要请求的数据
	 * @return json 
	 */
	 function postJson($url, $data = '', $timeout = 5){
		$postdata = http_build_query(
 
            $data
 
        );
        $opts = array('http' =>
 
                      array(
 
                          'method'  => 'POST',
 
                          'header'  => 'Content-type: application/x-www-form-urlencoded',
 
                          'content' => $postdata
                      )
        );
        $context = stream_context_create($opts);
        $result = file_get_contents($url, false, $context);
        return json_decode($result);
	}
	/**
	 * 将传入的数组格式化输出
	 * @param array $array 要输出的数组 
	 */
	function P($array){
		dump($array, 1, '<pre>', 0);
	}
	/**
	 * 浏览器友好的变量输出
	 * @param mixed $var 变量
	 * @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
	 * @param string $label 标签 默认为空
	 * @param boolean $strict 是否严谨 默认为true
	 * @return void|string
	 */
	function dump($var, $echo=true, $label=null, $strict=true) {
		$label = ($label === null) ? '' : rtrim($label) . ' ';
		if (!$strict) {
			if (ini_get('html_errors')) {
				$output = print_r($var, true);
				$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
			} else {
				$output = $label . print_r($var, true);
			}
		} else {
			ob_start();
			var_dump($var);
			$output = ob_get_clean();
			if (!extension_loaded('xdebug')) {
				$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
				$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
			}
		}
		if ($echo) {
			echo($output);
			return null;
		}else
			return $output;
	}
}
